import 'dart:async';

import 'package:eplorex/constants/app_constants.dart';
import 'package:eplorex/im/im_sdk_client.dart';
import 'package:eplorex/pages/community/moment/moment_create.dart';
import 'package:eplorex/pages/login/welcome_page.dart';
import 'package:eplorex/pages/message/collected_notification_page.dart';
import 'package:eplorex/pages/message/comment_notification_page.dart';
import 'package:eplorex/pages/message/notification/like_notification_page.dart';
import 'package:eplorex/pages/my/follow_page.dart';
import 'package:eplorex/pages/my/my_page.dart';
import 'package:eplorex/pages/splash_page.dart';
import 'package:eplorex/providers/auth_provider.dart';
import 'package:eplorex/providers/map_provider.dart';
import 'package:eplorex/utils/local_notification_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'models/bo/UnReadMessageCounter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  AMapProvider.instance.initialize();
  NotificationUtil().init();
  SharedPreferences prefs = await SharedPreferences.getInstance();
  runApp(MyApp());
  // affer
  delayTask();
}

void delayTask() {
  Future.delayed(Duration(seconds: 5), () {
    // IMSdkClient.initSDK();
    // IMSdkClient.login();
  });
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  // This widget is the root of your application.

  final routes = {
    '/': (context, {arguments}) => WelcomePage(),
    '/moment/create': (context, {arguments}) => MomentCreatePage(arguments: arguments),
    '/message/notification/like': (context, {arguments}) => LikeNotificationPage(),
    '/message/notification/comment': (context, {arguments}) => CommentNotificationPage(),
    '/message/notification/contact': (context, {arguments}) => FollowPage(),
    '/message/notification/collected': (context, {arguments}) => CollectedNotificationPage(),
    '/my': (context, {arguments}) => MyInfoPage(),
  };

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<AuthProvider>(create: (_) => AuthProvider()),
        ChangeNotifierProvider<UnReadMessageCounter>(create: (_) => UnReadMessageCounter())
      ],
      child: OKToast(
        dismissOtherOnShow: true,
        child: MaterialApp(
          localizationsDelegates: GlobalMaterialLocalizations.delegates,
          supportedLocales: const [
            // Locale('en', ''),
            Locale('zh', ''),
          ],
          title: AppConstants.appTitle,
          theme: ThemeData(
            primaryColor: Colors.grey,
            tabBarTheme: const TabBarTheme(dividerColor: Colors.transparent),
            // colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            // useMaterial3: true,
          ),
          home: SplashPage(),
          // routes: routes,
          onGenerateRoute: (RouteSettings settings) {
            //统一处理
            final String? name = settings.name;
            print(settings);
            final Function pageContentBuilder = this.routes[name] as Function;
            if (pageContentBuilder != null) {
              if (settings.arguments != null) {
                final Route route = MaterialPageRoute(
                    builder: (context) => pageContentBuilder(context,
                        arguments: settings.arguments));
                return route;
              } else {
                final Route route = MaterialPageRoute(
                    builder: (context) => pageContentBuilder(context));
                return route;
              }
            }
          },
        ),
      ),
    );
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);

    switch (state) {
      case AppLifecycleState.resumed:
        IMSdkClient.initSDK();
        IMSdkClient.login();
        break;
      case AppLifecycleState.inactive:
        print('enter 后台');
        break;
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        print('enter 不可见');
        break;
      // TODO: Handle this case.
    }
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance?.addObserver(this);
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addObserver(this);
  }
}
